﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DepartmentManage.aspx.cs" Inherits="Educational_School_DepartmentManage" %>
<script type="text/javascript">
window.addEvent('domready',function(){
    window.addFormCheckForDepartment=function(){
        new FormCheck('addFormForDepartment',{display:{tipsPosition:'left'},submit:false,onValidateSuccess:function(){
            MUI.showModalMessage('正在添加部门');
            new Request.JSON({url:'../Ajax/DepartmentManage.aspx?Action=saveAddDepartment',noCache:true,onSuccess:function(result){
                MUI.hideModalMessage();
                if(result.error)
                    MUI.appendStatusMessage({'text':result.msg});
                else
                {
                    MUI.appendStatusMessage({'text':result.msg});
                    window.loadDataForDepartment();
                }
	        },onFailure:function(error){
		        MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
		        MUI.hideModalMessage();
            }}).post($('addFormForDepartment'));
        }});   
    };
    
    window.updateFormCheckForDepartment=function(){
        new FormCheck('updateFormForDepartment',{display:{tipsPosition:'left'},submit:false,onValidateSuccess:function(){
            MUI.showModalMessage('正在修改部门');
            new Request.JSON({url:'../Ajax/DepartmentManage.aspx?Action=saveUpdateDepartment',noCache:true,onSuccess:function(result){
                MUI.hideModalMessage();
                if(result.error)
                    MUI.appendStatusMessage({'text':result.msg});
                else
                {
                    MUI.appendStatusMessage({'text':result.msg});
                    $('hiddenDepartmentName').set('value',$('updateFormForDepartment').getElement('input[name=departmentName]').get('value'));
                    window.loadDataForDepartment();
                }
	        },onFailure:function(error){
		        MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
		        MUI.hideModalMessage();
            }}).post($('updateFormForDepartment'));
        }});   
    };

    window.buildTreeList=function(data){
        var treeContainer=$('leftForDepartment');
        treeContainer.empty();
        var ul=new Element('ul',{id:'treeForDepartment','class': 'tree'});
        treeContainer.grab(ul);
        window.toTreeHtml(ul,data,-1);
        
    };
    
    window.toTreeHtml=function(parentContainer,data,departmentParentId){
        for(var i=0;i<data.length;i++)
        {
            if(data[i].departmentParentId==departmentParentId)
            {
                var isChild=false;
                for(j=0;j<data.length;j++)
                {
                    if(data[j].departmentParentId==data[i].departmentId)
                    {
                        isChild=true;
                        break;
                    }
                }
                var li=new Element('li',{'class':'folder'});
                var span=new Element('span');
                var a=new Element('a',{
                    text:data[i].departmentName,
                    events:{
                        click:function(){
                            var data=this.retrieve('data');
                            $('hiddenDepartmentId').set('value',data.departmentId);
                            $('hiddenDepartmentName').set('value',data.departmentName);
                            $('hiddenDepartmentParentId').set('value',data.departmentParentId);
                            $('btnUpdateDepartment').fireEvent('click');
                        }
                    }
                });
                a.store('data',data[i]);
                parentContainer.grab(li);
                li.grab(span);
                span.grab(a);
                if(isChild)
                {
                    li.addClass('f-open');
                    var ul=new Element('ul');
                    li.grab(ul);
                    window.toTreeHtml(ul,data,data[i].departmentId);
                }
            }
        }
    };
    
    window.getChildDepartmentIds=function(data,departmentIdArray,departmentParentId){
        for(var i=0;i<data.length;i++)
        {
            if(data[i].departmentParentId==departmentParentId)
            {
                departmentIdArray.include(data[i].departmentId);
                window.getChildDepartmentIds(data,departmentIdArray,data[i].departmentId);
            }
        }
    };
    
    window.addOptions=function(select,data,departmentParentId,level)
    {
        var j,temp;
        for(var i=0;i<data.length;i++)
        {
            temp='';
            for(var j=0;j<level;j++)
                temp+='－';
            if(data[i].departmentParentId==departmentParentId)
            {
                Util.addDistinctOption(select,data[i].departmentId,temp+data[i].departmentName);
                window.addOptions(select,data,data[i].departmentId,level+1);
            }
        }
    };

    window.layoutForDepartment=function(){
        new MUI.Column({
            container: $('leftForDepartment').getParent().getParent(),
		    id: 'leftColumnForDepartment',
		    placement: 'left',
		    width: 200,
		    resizeLimit: [100, 300]
	    });
	    new MUI.Column({
	        container: $('leftForDepartment').getParent().getParent(),
		    id: 'rightColumnForDepartment',
		    placement: 'main'
	    });
        new MUI.Panel({
            header: false,
		    id: 'leftPanelForDepartment',		
		    content: $('leftForDepartment'),
		    column: 'leftColumnForDepartment'
	    });
	    new MUI.Panel({
	        header: false,
		    id: 'rightPanelForDepartment',
		    content: $('rightForDepartment'),
		    column: 'rightColumnForDepartment',
		    padding: { top: 0, right: 0, bottom: 0, left: 0 }
	    });
    };
    
    window.loadDataForDepartment=function(){
        MUI.showModalMessage('正在加载部门数据');
        new Request.JSON({url:'../Ajax/DepartmentManage.aspx?Action=search',noCache:true,onSuccess:function(result){
            MUI.hideModalMessage();
            if(result.error)
                MUI.appendStatusMessage({'text':result.msg});
            else
            {
                window.data=result.data;
                window.buildTreeList(result.data);
                if (buildTree) buildTree('treeForDepartment');
                $('departmentParentId').empty();
                $('departmentParentId2').empty();
                window.addOptions('departmentParentId',result.data,-1,0);
                window.addOptions('departmentParentId2',result.data,-1,0);
                window.departmentOptionsHtml=$('departmentParentId2').get('html');
                if($('addFormForDepartment').getStyle('display')=='none')
                    $('treeForDepartment').getElement('a[text='+$('hiddenDepartmentName').get('value')+']').fireEvent('click');
                else
                {
                    $('treeForDepartment').getElement('a[text='+$('hiddenDepartmentName').get('value')+']').fireEvent('click');
                    $('btnAddDepartment').fireEvent('click');
                }
            }
		},onFailure:function(error){
		    MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
			MUI.hideModalMessage();
	    }}).get();
    };
    
    window.binddingEventForDepartment=function(){
        $('btnAddDepartment').addEvent('click',function(){
            var departmentId=$('hiddenDepartmentId').get('value');
            var departmentName=$('hiddenDepartmentName').get('value');
            var departmentParentId=$('hiddenDepartmentParentId').get('value');
            $('addFormForDepartment').show();
            $('updateFormForDepartment').hide();
            $('departmentParentId').set('value',departmentId);
            $('addFormForDepartment').getElement('input[name=departmentName]').set('value','');
        });
        $('btnUpdateDepartment').addEvent('click',function(){
            var departmentId=$('hiddenDepartmentId').get('value');
            var departmentName=$('hiddenDepartmentName').get('value');
            var departmentParentId=$('hiddenDepartmentParentId').get('value');
            if(departmentId=='0')
            {
                $('btnUpdateDepartment').hide();
                $('btnDeleteDepartment').hide();
                $('btnAddDepartment').show();
                $('btnAddDepartment').fireEvent('click');
            }
            else
            {
                $('btnUpdateDepartment').show();
                $('btnDeleteDepartment').show();
                $('btnAddDepartment').show();
                $('addFormForDepartment').hide();
                $('updateFormForDepartment').show();
                var updateDepartmentForm=$('updateFormForDepartment');
                var departmentParentId2=$('departmentParentId2');
                var departmentIdArray=[departmentId];
                departmentParentId2.empty();
                departmentParentId2.set('html',window.departmentOptionsHtml);
                window.getChildDepartmentIds(window.data,departmentIdArray,departmentId);
                Util.removeOptions(departmentParentId2,departmentIdArray);
                departmentParentId2.set('value',departmentParentId);
                updateDepartmentForm.getElement('input[name=departmentId]').set('value',departmentId);
                updateDepartmentForm.getElement('input[name=departmentName]').set('value',departmentName);          
            }
        });
        $('btnDeleteDepartment').addEvent('click',function(){
            MUI.showConfirm("你确认删除这个部门么？请确保此部门下没有子部门，没有与之相关职位。此操作不可恢复！",function(){
                MUI.showModalMessage('正在删除当前部门');
                new Request.JSON({
                    url:'../Ajax/DepartmentManage.aspx?Action=deleteDepartment&departmentId='+$('hiddenDepartmentId').get('value'),
                    noCache:true,
                    onSuccess:function(result){
                        MUI.hideModalMessage();
                        if(result.error)
                            MUI.appendStatusMessage({'text':result.msg});
                        else
                        {
                            window.loadDataForDepartment();
                            MUI.appendStatusMessage({'text':result.msg});
                        }
		            },onFailure:function(error){
		                MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
			            MUI.hideModalMessage();
	                }
	            }).get();
            });
        });
    };
    
    window.initializeForDepartment=function(){
        new ZebraTable({elements:'table.tableFixed'});
        window.layoutForDepartment();
        window.loadDataForDepartment();
        window.binddingEventForDepartment();
        window.addFormCheckForDepartment();
        window.updateFormCheckForDepartment();
        $('btnUpdateDepartment').fireEvent('click');
    };
    
    window.initForDepartment=function(){
        window.initializeForDepartment();
    };
    
    window.initForDepartment();
});
</script>
<div id="leftForDepartment"></div>
<div id="rightForDepartment">
    <div style="padding:10px;height:24px;">
        <input id="btnAddDepartment" type="button" class="float_left button" value="添加子部门" style="display:none;" />
        <input id="btnUpdateDepartment" type="button" class="float_left button" value="修改当前部门" style="display:none;margin-left:10px;" />
        <input id="btnDeleteDepartment" type="button" class="float_left button" value="删除当前部门" style="display:none;margin-left:10px;" />
    </div>
    <div>
        <form id="addFormForDepartment" style="display:none;">
        <div class="panel-header"><h2>添加子部门</h2></div>
        <table class="tableFixed" border="0" cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <td class="tableLeft1">上级部门名称：</td>
                <td class="tableLeft2"><select id="departmentParentId" name="departmentParentId" class="input"></select></td>
            </tr>
            <tr>
                <td class="tableLeft1">部门名称：</td>
                <td class="tableLeft2"><input class="input validate['required','length[2,10]']" name="departmentName" type="text" /></td>
            </tr>
            <tr>
                <td class="tableLeft1"></td>
                <td class="tableLeft2"><input class="submit button validate['submit']" type="button" value="提交添加" /></td>
            </tr>
        </table>
        </form>
        <form id="updateFormForDepartment" style="display:none;">
        <div class="panel-header"><h2>修改部门</h2></div>
        <input type="hidden" name="departmentId" />
        <table class="tableFixed" border="0" cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <td class="tableLeft1">上级部门名称：</td>
                <td class="tableLeft2"><select id="departmentParentId2" name="departmentParentId" class="input"></select></td>
            </tr>
            <tr>
                <td class="tableLeft1">部门名称：</td>
                <td class="tableLeft2"><input class="input validate['required','length[2,10]']" name="departmentName" type="text" /></td>
            </tr>
            <tr>
                <td class="tableLeft1"></td>
                <td class="tableLeft2"><input class="submit button validate['submit']" type="button" value="提交修改" /></td>
            </tr>
        </table>
        </form>
    </div>
</div>
<input type="hidden" id="hiddenDepartmentId" name="hiddenDepartmentId" value="0" />
<input type="hidden" id="hiddenDepartmentName" name="hiddenDepartmentName" value="<%=school.schoolName %>" />
<input type="hidden" id="hiddenDepartmentParentId" name="hiddenDepartmentParentId" value="-1" />